clear all
set more off
set maxvar 10000
version 13
set seed 12345


* Set directory 
* -------------

	global directory ""

* Subfolder globals
* -----------------

	global division			"${directory}/Division bias"
	global figures			"${directory}/Figures"
	global greenberg		"${directory}/Greenberg"
	global simulations		"${directory}/Simulations"
	global spatial 			"${directory}/Spatial"
	global tables			"${directory}/Tables"

	
* Stata tex
* -----------
do "${tables}/stata-tex.do"		


* Table H2: Division Bias, Simulations
* ------------------------------------
use "${directory}/ES_db.dta", clear  
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

local nsim = 200
gen sd_of_noise = 0.01
forvalues j = 1/`nsim' {
preserve
gen pop_noise = population + rnormal(0, sd_of_noise*population)
gen lnpop_noise = ln(pop_noise)
gen Y = ln(pecuniary_V1/pop_noise)
quietly reghdfe Y lnpop_noise , cluster(id3) abs(id3 year)
quietly parmest,format(estimate min95 max95 %8.3f p %8.3f) list(,)   norestore 
keep parm stderr estimate p  
keep in 1
save  "${division}/est_`j'.dta", replace 
restore	
}

clear   
local nsim = 200
forvalues i = 1/`nsim' {
append using  "${division}/est_`i'"
	}

sum estimate, det
insert_into_file using "sample_table.csv", key(B1) value(`r(p50)') format(%5.2f)
sum stderr, det
insert_into_file using "sample_table.csv", key(SE1) value(`r(p50)') format(%5.2f)


use "${directory}/ES_db.dta", clear  
cd "${tables}"

local nsim = 500
gen sd_of_noise = 0.05

forvalues j = 1/`nsim' {
preserve
gen pop_noise = population + rnormal(0, sd_of_noise*population)
gen lnpop_noise = ln(pop_noise)
gen Y = ln(pecuniary_V1/pop_noise)
quietly reghdfe Y lnpop_noise , cluster(id3) abs(id3 year)
quietly parmest,format(estimate min95 max95 %8.3f p %8.3f) list(,)   norestore 
keep parm stderr estimate p  
keep in 1
save  "${division}/est_`j'.dta", replace 
restore	
}

clear  
forvalues i = 1/`nsim' {
append using  "${division}/est_`i'"  
	}

sum estimate, det
insert_into_file using "sample_table.csv", key(B2) value(`r(p50)') format(%5.2f)
sum stderr, det
insert_into_file using "sample_table.csv", key(SE2) value(`r(p50)') format(%5.2f) 


use "${directory}/ES_db.dta", clear  
cd "${tables}"

local nsim = 500
gen sd_of_noise = 0.1

forvalues j = 1/`nsim' {
preserve
gen pop_noise = population + rnormal(0, sd_of_noise*population)
gen lnpop_noise = ln(pop_noise)
gen Y = ln(pecuniary_V1/pop_noise)
quietly reghdfe Y lnpop_noise , cluster(id3) abs(id3 year)
quietly parmest,format(estimate min95 max95 %8.3f p %8.3f) list(,)   norestore 
keep parm stderr estimate p  
keep in 1
save  "${division}/est_`j'.dta", replace 
restore	
}

clear  
forvalues i = 1/`nsim' {
append using  "${division}/est_`i'" 
	}

sum estimate, det
insert_into_file using "sample_table.csv", key(B3) value(`r(p50)') format(%5.2f)
sum stderr, det
insert_into_file using "sample_table.csv", key(SE3) value(`r(p50)') format(%5.2f)


use "${directory}/ES_db.dta", clear  
cd "${tables}"

local nsim = 500
gen sd_of_noise = 0.01

forvalues j = 1/`nsim' {
preserve
gen pop_noise = population + rnormal(0, sd_of_noise*population)
gen lnpop_noise = ln(pop_noise)
gen Y = ln(pecuniary_V1/pop_noise)
quietly ivreghdfe Y (lnpop_noise=IV) , cluster(id3) abs(id3 year)
quietly parmest,format(estimate min95 max95 %8.3f p %8.3f) list(,)   norestore 
keep parm stderr estimate p  
keep in 1
save  "${division}/est_`j'.dta", replace 
restore	
}

clear 
forvalues i = 1/`nsim' {
append using  "${division}/est_`i'"  
	}

sum estimate, det
insert_into_file using "sample_table.csv", key(B4) value(`r(p50)') format(%5.2f)
sum stderr, det
insert_into_file using "sample_table.csv", key(SE4) value(`r(p50)') format(%5.2f)


use "${directory}/ES_db.dta", clear  
cd "${tables}"

local nsim = 500
gen sd_of_noise = 0.05

forvalues j = 1/`nsim' {
preserve
gen pop_noise = population + rnormal(0, sd_of_noise*population)
gen lnpop_noise = ln(pop_noise)
gen Y = ln(pecuniary_V1/pop_noise)
quietly ivreghdfe Y (lnpop_noise=IV) , cluster(id3) abs(id3 year)
quietly parmest,format(estimate min95 max95 %8.3f p %8.3f) list(,)   norestore 
keep parm stderr estimate p  
keep in 1
save  "${division}/est_`j'.dta", replace 
restore	
}

clear 
forvalues i = 1/`nsim' {
append using  C:\Users\sv100354\Dropbox\Recherche\Paper_Crime\2.Data\Repository\DivisionBias\est_`i'  
	}

sum estimate, det
insert_into_file using "sample_table.csv", key(B5) value(`r(p50)') format(%5.2f)
sum stderr, det
insert_into_file using "sample_table.csv", key(SE5) value(`r(p50)') format(%5.2f)


use "${directory}/ES_db.dta", clear  
cd "${tables}"

local nsim = 500
gen sd_of_noise = 0.10

forvalues j = 1/`nsim' {
preserve
gen pop_noise = population + rnormal(0, sd_of_noise*population)
gen lnpop_noise = ln(pop_noise)
gen Y = ln(pecuniary_V1/pop_noise)
quietly ivreghdfe Y (lnpop_noise=IV) , cluster(id3) abs(id3 year)
quietly parmest,format(estimate min95 max95 %8.3f p %8.3f) list(,)   norestore 
keep parm stderr estimate p  
keep in 1
save  "${division}/est_`j'.dta", replace 
restore	
}

clear  
forvalues i = 1/`nsim' {
append using C:\Users\sv100354\Dropbox\Recherche\Paper_Crime\2.Data\Repository\DivisionBias\est_`i'  
	}

sum estimate, det
insert_into_file using "sample_table.csv", key(B6) value(`r(p50)') format(%5.2f)
sum stderr, det
insert_into_file using "sample_table.csv", key(SE6) value(`r(p50)') format(%5.2f)

 
cd "${tables}"
table_from_tpl, t(TPL_DBias.tex) r(sample_table.csv) o(T_DBias.tex)
